﻿//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Expenses.ViewModels;
using Expenses.ExpenseService;
using System.Collections.ObjectModel;
using Windows.ApplicationModel.DataTransfer;
using Callisto.Controls;
using Windows.UI;

namespace Expenses.Views
{
    public sealed partial class ChargesPage : Expenses.Common.LayoutAwarePage
    {
        ChargesViewModel chargesViewModel = null;
        private DataTransferManager dataTransferManager = null;

        public ChargesPage()
        {
            this.InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            GetCharges();

            // Register handler for DataRequested events for sharing
            dataTransferManager = DataTransferManager.GetForCurrentView();
            dataTransferManager.DataRequested += new TypedEventHandler<
                DataTransferManager, DataRequestedEventArgs>(OnDataRequested);
        }

        private async void GetCharges()
        {
            ChargesProgressRing.IsActive = true;

            chargesViewModel = new ChargesViewModel();
            ObservableCollection<ChargeViewModel> charges =
                await chargesViewModel.GetCharges();
            ChargesViewSource.Source = charges;

            if (charges.Count() > 0)
            {
                PageTitleTextBlock.Text = string.Format(
                    "Charges ({0} for {1:C})", charges.Count(),
                    chargesViewModel.AmountLT30Days + chargesViewModel.Amount30To44Days +
                    chargesViewModel.AmountGT45Days);
            }
            else
            {
                PageTitleTextBlock.Text = "You have no charges";
            }
            ChargesProgressRing.IsActive = false;
        }

        private void BackButton_Click(object sender, RoutedEventArgs e)
        {
            this.Frame.Navigate(typeof(MainPage));
        }

        private void SelectAllButton_Click(object sender, RoutedEventArgs e)
        {
            ChargesGridView.SelectAll();
        }

        private void ClearSelectionButton_Click(object sender, RoutedEventArgs e)
        {
            ChargesGridView.SelectedItem = null;
        }

        private void ChargesGridView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ChargesGridView.SelectedItems.Count() > 0)
            {
                ChargesAppBar.IsOpen = true;
                ChargesAppBar.IsSticky = true;
                ClearSelectionButton.Visibility = Visibility.Visible;
                AddToReportButton.Visibility = Visibility.Visible;
            }
            else
            {
                ChargesAppBar.IsOpen = false;
                ChargesAppBar.IsOpen = false;
                ClearSelectionButton.Visibility = Visibility.Collapsed;
                AddToReportButton.Visibility = Visibility.Collapsed;
            }
        }

        private void ChargesGridView_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.Frame.Navigate(typeof(ChargePage), e.ClickedItem);
        }

        private void NewChargeButton_Click(object sender, RoutedEventArgs e)
        {
            this.Frame.Navigate(typeof(ChargePage));
        }

        private void AddToReportButton_Click(object sender, RoutedEventArgs e)
        {
            if (ChargesGridView.SelectedItems.Count() == 0)
            {
                var flyout = new Flyout();
                flyout.Content = new TextBlock()
                {
                    Text = "select at least one charge",
                    Foreground = new SolidColorBrush(Colors.Black),
                    Margin = new Thickness(10, 0, 10, 0),
                    VerticalAlignment = VerticalAlignment.Center,
                    FontSize = 18
                };
                flyout.Placement = PlacementMode.Top;
                flyout.PlacementTarget = AddToReportButton;
                flyout.IsOpen = true;
                return;
            }
            App.SelectedCharges = new List<ChargeViewModel>();
            foreach (ChargeViewModel charge in ChargesGridView.SelectedItems)
            {
                App.SelectedCharges.Add(charge);
            }

            this.Frame.Navigate(typeof(ExpenseReportPage));
        }

        void OnDataRequested(DataTransferManager sender, DataRequestedEventArgs args)
        {
            var request = args.Request;
            request.FailWithDisplayText("This page does not support sharing.");
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            dataTransferManager.DataRequested -= new TypedEventHandler<
                DataTransferManager, DataRequestedEventArgs>(OnDataRequested);
            App.LastPage = "ChargesPage";
        }

    }
}
